﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MetaView.Ticket.Common;
using MetaView.Ticket.Models.Pages;
using MetaView.Ticket.Tools.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Reflection;
using System.Windows.Input;
using Wpf.Ui.Abstractions.Controls;
using Wpf.Ui.Appearance;

namespace MetaView.Ticket.ViewModels.Pages
{
    /// <summary>
    /// 设置页面视图模型
    /// </summary>
    public partial class SettingsViewModel : ObservableObject, INavigationAware
    {
        /// <summary>
        /// 设置页面模型
        /// </summary>
        [ObservableProperty]
        public SettingsPageModel? settingsPageModels;

        /// <summary>
        /// 软件版本号
        /// </summary>
        [ObservableProperty]
        private string appVersion = string.Empty;

        /// <summary>
        /// 当前主题
        /// </summary>
        [ObservableProperty]
        private ApplicationTheme currentTheme;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SettingsViewModel()
        {
            SettingsPageModels = new SettingsPageModel();

            #region 绑定当前系统主题为主题列表的默认值
            switch (ApplicationThemeManager.GetAppTheme().ToString())
            {
                case "Light":
                    SettingsPageModels.SelectedTheme = SettingsPageModels.ThemeList.FirstOrDefault(t => t.Code == "Light");
                    break;
                case "Dark":
                    SettingsPageModels.SelectedTheme = SettingsPageModels.ThemeList.FirstOrDefault(t => t.Code == "Dark");
                    break;
                case "System":
                    SettingsPageModels.SelectedTheme = SettingsPageModels.ThemeList.FirstOrDefault(t => t.Code == "System");
                    break;
                default:
                    SettingsPageModels.SelectedTheme = SettingsPageModels.ThemeList.FirstOrDefault(t => t.Code == "Light");
                    break;
            }
            #endregion

            //获取软件版本号
            string versionStr = Assembly.GetExecutingAssembly().GetName().Version?.ToString()!;
            AppVersion = $"V - {versionStr}";
        }

        #region 切换软件主题
        /// <summary>
        /// 切换软件主题
        /// </summary>
        /// <param name="parameter"></param>
        [RelayCommand]
        private void ChangeAppTheme(ThemeModel themeModel)
        {
            //读取文件内容
            string fileContent = File.ReadAllText(GlobalStaticData.AppSettingsFilePath);

            //解析文件内容
            JObject? jObject = JObject.Parse(fileContent);
            JToken? jToken = jObject?["AppSettings"]?["Theme"];
            jToken?.Replace(themeModel.Code);

            switch (themeModel.Code)
            {
                case "Light":
                    if (CurrentTheme == ApplicationTheme.Light) break;
                    ApplicationThemeManager.Apply(ApplicationTheme.Light);
                    CurrentTheme = ApplicationTheme.Light;
                    break;
                case "Dark":
                    if (CurrentTheme == ApplicationTheme.Dark) break;
                    ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                    CurrentTheme = ApplicationTheme.Dark;
                    break;
                default:
                    string systemTheme = SystemHelper.GetSystemTheme();

                    if (systemTheme == "Dark")
                    {
                        if (CurrentTheme == ApplicationTheme.Dark) break;
                        ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                        CurrentTheme = ApplicationTheme.Dark;                    }
                    else
                    {
                        if (CurrentTheme == ApplicationTheme.Light) break;
                        ApplicationThemeManager.Apply(ApplicationTheme.Light);
                        CurrentTheme = ApplicationTheme.Light;                        
                    }
                    break;
            }

            //保存中英文到配置文件中
            File.WriteAllText(GlobalStaticData.AppSettingsFilePath, jObject?.ToString());
        }
        #endregion

        public Task OnNavigatedToAsync()
        {
            return Task.CompletedTask;
        }

        public Task OnNavigatedFromAsync()
        {
            return Task.CompletedTask;
        }
    }
}
